package com.pro.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.dao.BookMapper;
import com.pro.dao.ShoucangMapper;
import com.pro.domain.Book;
import com.pro.domain.Shoucang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ShoucangServiceImpl implements ShoucangService {
    @Autowired
    private ShoucangMapper shoucangMapper;
    @Autowired
    private BookMapper bookMapper;

    /*根据bookid和memberid查有没有收藏过*/
    @Override
    public Integer selectOne(Long bookId, Long memberId) {
        QueryWrapper<Shoucang> queryWrapper = new QueryWrapper<Shoucang>();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("member_id",memberId);
        Shoucang shoucang = shoucangMapper.selectOne(queryWrapper);
        /*如果没有收藏过，就让页面变成已收藏，然后调用insert方法存入数据库*/
        if(shoucang==null){
            shoucang = new Shoucang();
            shoucang.setBookId(bookId);
            shoucang.setMemberId(memberId);
            shoucangMapper.insert(shoucang);
            return 1;/*已收藏*/
        }else{
            shoucang = shoucangMapper.selectOne(queryWrapper);
            shoucangMapper.deleteById(shoucang.getShoucangId());
            return 2;/*取消收藏*/
        }
    }

    /*根据bookid和memberid查有没有收藏过   只查，不修改*/
    @Override
    public Integer selectTwo(Long bookId, Long memberId) {
        QueryWrapper<Shoucang> queryWrapper = new QueryWrapper<Shoucang>();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("member_id",memberId);
        Shoucang shoucang = shoucangMapper.selectOne(queryWrapper);
        /*如果没有收藏过，就让页面变成已收藏，然后调用insert方法存入数据库*/
        if(shoucang==null){
            return 1;/*还没收藏*/
        }else{
            return 2;/*已经收藏了*/
        }
    }

    @Override
    public List<Shoucang> getBookIdByMemberId(Long memberId) {
        QueryWrapper<Shoucang> queryWrapper = new QueryWrapper<Shoucang>();
        queryWrapper.eq("member_id",memberId);
        List<Shoucang> shoucangList = shoucangMapper.selectList(queryWrapper);
        return shoucangList;
    }

}
